package com.yz;

import com.yz.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.RequestEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author yuanzheng
 * @date 2020/7/7-11:46
 */
public class TestGet {
    @Autowired
    private RestTemplate restTemplate;

    String uri = "";

    @GetMapping("/getForObject")
    public User getUser1() {
        // 1-getForObject()
        User user1 = this.restTemplate.getForObject(uri, User.class);
        return user1;
    }

    @GetMapping("/getForEntity")
    public User getUser2() {
        // 2-getForEntity()
        ResponseEntity<User> responseEntity1 = restTemplate.getForEntity(uri, User.class);
        HttpStatus statusCode = responseEntity1.getStatusCode();
        HttpHeaders header = responseEntity1.getHeaders();
        User user2 = responseEntity1.getBody();
        return user2;
    }

    @GetMapping("/exchange")
    public User getUser3() throws URISyntaxException {
        // 3-exchange()
        RequestEntity requestEntity = RequestEntity.get(new URI(uri)).build();
        ResponseEntity<User> responseEntity2 = restTemplate.exchange(requestEntity, User.class);
        User user3 = responseEntity2.getBody();
        return user3;
    }

    @GetMapping("/exchange2")
    public User getUser4() {
        User user = restTemplate.getForObject("http://fantj.top/notice/list/{1}/{2}", User.class, 1, 5);
        return user;
    }

    @GetMapping("/exchange3")
    public User getUser5() {
        Map<String, String> map = new HashMap();
        map.put("start", "1");
        map.put("page", "5");
        User user = restTemplate.getForObject("http://fantj.top/notice/list/", User.class, map);
        return user;
    }

}
